/*=============================================================================+
|                                                                              |
| Copyright 2015                                                             |
| Montage Inc. All right reserved.                                             |
|                                                                              |
+=============================================================================*/
/*! 
*   \file app_init.c
*   \brief main entry
*   \author Montage
*/

/*=============================================================================+
| Included Files                                                               |
+=============================================================================*/
#include <stdint.h>
#include <stdarg.h>
#include <event.h>
#include <common.h>
#include <net_api.h>
#include <wla_api.h>
#include <cfg_api.h>
#include <gpio.h>
#include <version.h>
#include <built_info.h>
#include <omniconfig.h>

#if defined(CONFIG_FREERTOS)
#include <FreeRTOS.h>
#include <task.h>
#endif
#if defined(CONFIG_LWIP)
#include <net_api.h>
#endif
#include <user_config.h>
#include <mylinks_wifi.h>
#include <smartconfig.h>
#include <lwip/sockets.h>
#include <omniconfig.h>
extern struct serial_buffer *ur0_rxbuf;

int app_main(void)
{
#ifdef CONFIG_WLA_LED
	pin_mode(WIFI_LED_PIN, 1);
	digital_write(WIFI_LED_PIN, 0);
#endif
	/* Do not add any process here, user should add process in user_thread */
	hw_sys_init();
	return PT_EXITED;
}

//串口接收回调函数
void uart0_rev( void * arg){
	uint8_t temp;

	for(;;){
		//等待串口是否有数据传入
		if(0 != uart_recv_sem_wait(100)){
			continue;
		}
		//判断串口数据是否为空
		while(serial_buffer_empty(ur0_rxbuf)){
			//读取一个字节的串口数据
			temp = serial_buffer_getchar(ur0_rxbuf);			
		}
	}
}

static void omniconfig_done(char *ssid, char *key, int mode,int error)
{
	struct station_config new;
	if (OMNI_RET_SUCCESS == error) {
		wifi_station_get_config(&new);
		strcpy(new.ssid,ssid);
		//设置连接的路由器密码
		strcpy(new.password,key);
		wifi_station_set_config(&new);
		omnicfg_connect();
		omnicfg_stop();
	}else if (OMNI_RET_TIMEOUT == error) {
		uart0_sendStr("Result    : OMNICONFIG Timeout\n");
	} else if (OMNI_RET_ERR_SSID == error) {
		uart0_sendStr("Result    : SSID Length is too long ! \n");
		uart0_sendStr("          : SSID Length need be small than 32 .\n");
	} else if (OMNI_RET_ERR_PWD == error) {
		uart0_sendStr("Result    : PWD Length is too long ! \n");
		uart0_sendStr("          : PWD Length need be small than 64.\n");
	} else if (OMNI_RET_ERR_UNKNOW == error) {
		uart0_sendStr("Result    : INTERNAL ERROR\n");
	}
}


static void uart_init(void)
{
	/*
	设置串口参数：
	参数1->波特率：115200
	参数2->校验位：无
	参数3->停止位：1位
	参数4->串口号：0号串口
	*/
	serial_conf(baudrate_select(115200), 0, 1, 0,0 );
	serial_init(0);

}


void user_init(void){
	uart_init();
	//注册一个串口0的接收任务进行数据接收
	//设置模块为STA工作模式
	wifi_set_opmode(OPMODE_STA);
	uart0_sendStr("start omniconfig\r\n");
	uart0_rev_register(uart0_rev);
	omnicfg_start(omniconfig_done,360*1000);
	return;
}



